package com.mtl.inmigrant.shoes;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.mtl.inmigrant.shoes.adapter.OnCustomClickListener;
import com.mtl.inmigrant.shoes.adapter.TaskListAdapter;
import com.mtl.inmigrant.shoes.entity.ShoeActionsInmigrant;
import com.mtl.inmigrant.shoes.entity.TypeActivityShoes.typeActionShoe;
import com.mtl.inmigrant.shoes.sqlite.ShoesDataSource;

public class TaskImmigrantListActivity extends Activity implements OnCustomClickListener {
	
	private static final String TAG = TaskImmigrantListActivity.class.getName();
	private ShoesDataSource dataSource;
	private TaskListAdapter adapter;
	private List<ShoeActionsInmigrant> values = null;
	private String idAct;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        setContentView(R.layout.tasks_list);
        ListView lv = (ListView) findViewById(R.id.list);
        

		Button btnCancel = (Button) findViewById(R.id.btn_cancel_task);
		
        dataSource = new ShoesDataSource(this);
		dataSource.open();
		idAct = extras.getString("idActivity");
		values = dataSource.getActionEnfants(new String[]{idAct}, typeActionShoe.ACTIVITY.toString());
        
		adapter = new TaskListAdapter(getApplicationContext(), values, dataSource, this, idAct);
		lv.setAdapter(adapter);
		//dataSource.close();
		
		btnCancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Close the application
				finish();
			}
		});
	}
	
	public void OnCustomClick(View aView, int position) {
		ShoeActionsInmigrant element = (ShoeActionsInmigrant) adapter.getItem(position); 
		if (element != null){
			Bundle bundle = new Bundle();
			bundle.putString("idTask", element.getIdActivity());
			bundle.putString("idActivity", idAct);
			Intent i = new Intent(TaskImmigrantListActivity.this, ReferentialImmigrantListActivity.class);
			i.putExtras(bundle);
			startActivity(i);
			//Toast.makeText(getApplicationContext(), element.getNameAction() + " " + + position, Toast.LENGTH_LONG).show();
		}
		else
			Toast.makeText(getApplicationContext(), " No tasks created!!!" , Toast.LENGTH_LONG).show();
	}

	 @Override
	 protected void onDestroy() {
	  // TODO Auto-generated method stub
	  super.onDestroy();
	  dataSource.close();
	 }
}
